using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Threading.Tasks;
using EmotionChat.Services;
using Mvvm.Models;
using Mvvm.Services;

namespace Mvvm.ViewModels
{
    public class DepressionAnalysisViewModel : PageViewModelBase
    {
        private readonly IChatDataProcessedStorage _chatDataProcessedStorage;
        private readonly IXunFeiClient _aiService;

        private string _analysisResult;
        public string AnalysisResult
        {
            get => _analysisResult;
            set => this.RaiseAndSetIfChanged(ref _analysisResult, value);
        }

        private bool _isLoading;
        public bool IsLoading
        {
            get => _isLoading;
            set => this.RaiseAndSetIfChanged(ref _isLoading, value);
        }

        public DepressionAnalysisViewModel(IChatDataProcessedStorage chatDataProcessedStorage, IXunFeiClient aiService)
        {
            _chatDataProcessedStorage = chatDataProcessedStorage;
            _aiService = aiService;

            // 在ViewModel初始化时自动开始分析
            AnalyzeChatDataAsync();
        }

        private async void AnalyzeChatDataAsync()
        {
            try
            {
                IsLoading = true;
                var chatDataList = new ChatDataProcessedStorage();
                // 获取处理后的聊天记录
                var processedDataList = await chatDataList.ListAsync();

                if (processedDataList == null || processedDataList.Count == 0)
                {
                    AnalysisResult = "暂无处理后的聊天记录可供分析。";
                    return;
                }

                // 这里假设分析最新的一条记录，您可以根据需求调整
                ChatProcessedData latestData = processedDataList.OrderByDescending(c => c.LastChatTime).FirstOrDefault();

                if (latestData == null)
                {
                    AnalysisResult = "暂无有效的聊天记录可供分析。";
                    return;
                }

                // 生成提示词
                string prompt = GeneratePrompt(latestData);
                // 调用AI方法
                var _aiService = new XunFeiClient();
                string aiResponse = await _aiService.GetResponseAsync(prompt);

                // 设置分析结果
                AnalysisResult = aiResponse;
            }
            finally
            {
                IsLoading = false;
            }
        }

        private string GeneratePrompt(ChatProcessedData data)
        {
            // 根据ChatProcessedData生成适合AI分析的提示词
            return $"请根据以下聊天记录数据，评估用户的抑郁程度，并给出评分、分析原因和建议。\n\n" +
                   $"总消息数: {data.TotalChatCount}\n" +
                   $"总字数: {data.TotalWordCount}\n" +
                   $"用户1: {data.Username1} - 消息数: {data.User1MessageCount}, 字数: {data.User1WordCount}\n" +
                   $"用户2: {data.Username2} - 消息数: {data.User2MessageCount}, 字数: {data.User2WordCount}\n" +
                   $"积极消息: {data.PositiveMessageCount}, 中性消息: {data.NeutralMessageCount}, 消极消息: {data.NegativeMessageCount}\n" +
                   $"用户1积极消息: {data.User1PositiveMessageCount}, 中性消息: {data.User1NeutralMessageCount}, 消极消息: {data.User1NegativeMessageCount}\n" +
                   $"用户2积极消息: {data.User2PositiveMessageCount}, 中性消息: {data.User2NeutralMessageCount}, 消极消息: {data.User2NegativeMessageCount}\n" +
                   $"首次聊天时间: {data.FirstChatTime}, 最后聊天时间: {data.LastChatTime}\n" +
                   $"总聊天时长（小时）: {data.TotalDuration}, 平均聊天时长（小时）: {data.AverageDuration}, 平均聊天间隔（小时）: {data.AverageInterval}\n" +
                   $"对话次数: {data.ConversationCount}\n" +
                   $"用户1平均回复时间（分钟）: {data.User1AverageReplyTime}, 用户2平均回复时间（分钟）: {data.User2AverageReplyTime}\n" +
                   $"用户1快速回复次数（<1分钟）: {data.User1LessThanOneMinute}, 1-5分钟: {data.User1LessThanFiveMinutes}, 5-10分钟: {data.User1LessThanTenMinutes}, >10分钟: {data.User1MoreThanTenMinutes}\n" +
                   $"用户2快速回复次数（<1分钟）: {data.User2LessThanOneMinute}, 1-5分钟: {data.User2LessThanFiveMinutes}, 5-10分钟: {data.User2LessThanTenMinutes}, >10分钟: {data.User2MoreThanTenMinutes}\n" +
                   $"情绪分布 - 喜爱: {data.LoveMessageCount}, 愉快: {data.HappyMessageCount}, 正常: {data.NormalMessageCount}, 愤怒: {data.AngryMessageCount}, 厌恶: {data.DisgustMessageCount}, 恐惧: {data.FearMessageCount}, 悲伤: {data.SadMessageCount}\n" +
                   $"用户1情绪分布 - 喜爱: {data.User1LoveMessageCount}, 愉快: {data.User1HappyMessageCount}, 正常: {data.User1NormalMessageCount}, 愤怒: {data.User1AngryMessageCount}, 厌恶: {data.User1DisgustMessageCount}, 恐惧: {data.User1FearMessageCount}, 悲伤: {data.User1SadMessageCount}\n" +
                   $"用户2情绪分布 - 喜爱: {data.User2LoveMessageCount}, 愉快: {data.User2HappyMessageCount}, 正常: {data.User2NormalMessageCount}, 愤怒: {data.User2AngryMessageCount}, 厌恶: {data.User2DisgustMessageCount}, 恐惧: {data.User2FearMessageCount}, 悲伤: {data.User2SadMessageCount}\n\n" +
                   $"请基于以上数据，提供抑郁程度评分（0-10），分析原因，并给出具体建议。";
        }

        public override bool CanNavigateNext { get; protected set; }
        public override bool CanNavigatePrevious { get; protected set; }
    }
}
